package No1_100.No61_70.AddBinary_67;

public class Solution {
	public static String addBinary(String a, String b) {
		int flag = 0;
		int min_length = 0;
        int max_length = 0;
        String max = null;
		if (a.length()>b.length()) {
			max_length = a.length();
			max = a;
			min_length = b.length();
		}else {
			max_length = b.length();
			max = b;
			min_length = a.length();
		}
		System.out.println(max_length+","+min_length);
		char[] result = new char[max_length];
        for (int i = 0; i < min_length; i++) {
        	int temp = a.charAt(a.length()-1-i)-48 + b.charAt(b.length()-1-i)-48 + flag;
			if (temp == 2) {
				temp = 0;
				flag = 1;
			}else if (temp == 3) {
				temp = 1;
				flag = 1;
			}
			else {
				flag = 0;
			}
			result[i] = (char) (temp + 48);
			System.out.println(i+":"+result[i]);
		}
        for (int i = min_length; i < max_length; i++) {
			int temp = max.charAt(max_length-1-i)-48 + flag;
			if (temp == 2) {
				temp = 0;
				flag = 1;
			}
			else {
				flag = 0;
			}
			result[i] =  (char) (temp + 48);
			System.out.println(i+":"+result[i]);
		}
        if (flag == 1) {
			char[] back = new char[max_length+1];
			back[0] = 49;
			for (int i = 1; i < back.length; i++) {
				back[i] = result[max_length-i];
//				System.out.println(back[i]);
			}
			return String.valueOf(back);
		}
        else{
        	char[] back = new char[max_length];
			for (int i = 0; i < back.length; i++) {
				back[i] = result[max_length-1-i];
//				System.out.println(back[i]);
			}
        	return String.valueOf(back);
        }
    }
	
	public static void main(String[] args) {
		//String a = "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101";
		//String b = "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011";
		String a = "1111";
		String b = "1111";
//		addBinary(a,b);
		System.out.println(addBinary(a,b));
	}
}
